package com.sanben.user.controller;

import com.sanben.common.web.domain.Result;
import com.sanben.common.web.entity.UserVO;
import com.sanben.user.data.dto.UserDto;
import com.sanben.user.data.dto.UserEditPasswordDto;
import com.sanben.user.data.dto.UserLoginDto;
import com.sanben.user.entity.User;
import com.sanben.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author cyh
 * @description: 用户表 控制器
 * @date: 2020-08-11
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户表相关接口")
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public Result<UserVO> login(@RequestBody UserLoginDto dto) {
        return Result.data(userService.login(dto));
    }

    @GetMapping("/captcha/{randomStr}")
    @ApiOperation(value = "获取图形验证码")
    public void captcha(HttpServletResponse httpServletResponse,
                        @PathVariable String randomStr) {
        userService.captcha(httpServletResponse, randomStr);
    }

    @PostMapping("/mobileLogin")
    @ApiOperation(value = "手机号登录")
    public Result<UserVO> mobileLogin(@RequestParam String mobile,
                                      @RequestParam String code,
                                      @RequestParam Integer regionId
                                      ) {
        return Result.data(userService.mobileLogin(mobile, code,regionId));
    }

    @GetMapping("/getMobileCode/{mobile}")
    @ApiOperation(value = "获取手机号验证码")
    public Result<Boolean> getMobileCode(@PathVariable String mobile) {
        return Result.data(userService.getMobileCode(mobile));
    }

    @PostMapping("/register")
    @ApiOperation(value = "注册")
    public Result register(@RequestParam String mobile, @RequestParam String code,
                           @RequestParam String password) {
        userService.register(mobile, code, password);
        return Result.data();
    }

    @PostMapping("/edit/password")
    @ApiOperation(value = "修改密码")
    public Result editPassword(@Validated @RequestBody UserEditPasswordDto dto) {
        userService.editPassword(dto);
        return Result.data();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "详情")
    public Result<User> detail(@PathVariable Integer id) {
        return Result.data(userService.detail(id));
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public Result update(@RequestBody UserDto dto) {
        return Result.data(userService.updateById(dto));
    }

    @PostMapping("/loginOut/{userId}")
    @ApiOperation(value = "修改")
    public Result loginOut(@PathVariable Integer userId) {
        userService.loginOut(userId);
        return Result.data();
    }
}
